package com.yx.cloud.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.recyclerview.widget.RecyclerView;

import com.iot.product.sweeper.config.SweeperProductConfig;
import com.yx.cloud.Interface.OnItemClickListener;
import com.yx.cloud.R;
import com.yx.cloud.product.ProductConfigConstants;
import com.yx.cloud.product.ProductManager;
import com.yx.cloud.utils.RomUtils;
import com.iot.product.sweeper.bean.Robot;

import java.util.List;

public class MainAddAdapter extends RecyclerView.Adapter implements View.OnClickListener {
    private final static String TAG = "MainAddAdapter";
    private Context mContext;
    private List<Robot> mList;
    private LayoutInflater mInflater;
    private OnItemClickListener onItemClickListener;
    private View view1, view2;

    public MainAddAdapter(Context context, List<Robot> list) {
        mContext = context;
        mInflater = LayoutInflater.from(context);
        mList = list;
    }

    public enum ItemType {
        ITEM1, ITEM2
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == ItemType.ITEM1.ordinal()) {
            view1 = mInflater.inflate(R.layout.add_equipment_adapter_item, parent, false);
            AddViewHolder addViewHolder = new AddViewHolder(view1);
            view1.setOnClickListener(this);
            return addViewHolder;
        } else if (viewType == ItemType.ITEM2.ordinal()) {
            view2 = mInflater.inflate(R.layout.add_equipment_adapter_item2, parent, false);
            ViewHolder viewHolder2 = new ViewHolder(view2);
            view2.setOnClickListener(this);
            return viewHolder2;
        }
        return null;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof AddViewHolder) {
            //TODO
        } else if (holder instanceof ViewHolder) {
            Robot bean = mList.get(position - 1);
            ((ViewHolder) holder).tvRobotName.setText(bean.getName());
            // 根据pid判断机器型号，进行分类显示
            ((ViewHolder) holder).ivRobotIcon.setImageResource(bean.getConfig().optInt(ProductConfigConstants.robot_Preview));
//            if (bean.getEquipmentType() == 0) {
//                ((ViewHolder) holder).ivRobotIcon.setImageResource(((SweeperProductConfig) beangetCleanSettings.getConfig()).robot_Preview);
//            } else {
//                ((ViewHolder) holder).ivRobotIcon.setImageResource(R.mipmap.robot_icon_pic_x5);
//            }
        }
        holder.itemView.setTag(position + 1);
    }


    @Override
    public int getItemCount() {
        return mList.size() + 1;
    }

    @Override
    public int getItemViewType(int position) {
        if (position == 0) {
            return ItemType.ITEM1.ordinal();
        } else {
            return ItemType.ITEM2.ordinal();
        }
    }

    @Override
    public void onClick(View v) {
        if (RomUtils.isFastClick()) {
            return;
        }
        if (onItemClickListener != null) {
            onItemClickListener.onItemClick(v, (Integer) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        onItemClickListener = listener;
    }

    /**
     * 第2种item对应的holder
     */
    class ViewHolder extends RecyclerView.ViewHolder {
        TextView tvRobotName;
        ImageView ivRobotIcon;

        public ViewHolder(View itemView) {
            super(itemView);
            tvRobotName = itemView.findViewById(R.id.tv_robotName);
            ivRobotIcon = itemView.findViewById(R.id.iv_robotIcon);
        }
    }

    /**
     * 第1种item对应的holder
     */
    class AddViewHolder extends RecyclerView.ViewHolder {
        ImageView ivRobotIcon;

        public AddViewHolder(View itemView) {
            super(itemView);
            ivRobotIcon = itemView.findViewById(R.id.iv_robotIcon);
        }
    }

}
